package com.kms.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.jcraft.jsch.JSchException;
import com.kms.console.JSchConsole;
import com.kms.derby.Connections;
import com.kms.model.MGroup;
import com.kms.model.MSyspara;
import com.kms.model.SystemParameterBean;
import com.kms.service.SystemparameterService;

public class LogServlet extends HttpServlet{
	SystemparameterService sysservice ;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		HttpSession session = request.getSession();
		JSchConsole jc = new JSchConsole();
//		String command = "";
//		command = "cd dpdir;zip exim_kms.zip exim_kms_20110705.dmp";
//		command = "exp kms/kms@kobra file=/home/kms/001.dmp tables=accentry2 log=/home/kms/001.log";
		StringBuffer command = new StringBuffer();
		//linux unix 
//		command.append("exp kms/kms@kobra file=/home/kms/accentry2.dmp tables=accentry2 query="+"\\"+"\"where settledate \\= 20110702"+"\\"+"\" log=/home/kms/accentry2.log ;");
//		command.append("exp kms/kms@kobra file=/home/kms/balance.dmp tables=balance query="+"\\"+"\"where settledate \\= 20110702"+"\\"+"\" log=/home/kms/balance.log ;");
		//windows
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
		String datetime = sdf.format(new Date());
		String host = "";
		String user = "";
		String pwd = "";
		String dir = "";
		String dirctory = "";
		String commandstr ="";
		int port = 0;
		
		
		String id = request.getParameter("id");
		MSyspara mssysapp=new MSyspara();
		MSyspara mssysdb=new MSyspara();
		MGroup mgroup = new MGroup();
		if(id !=null && !"null".equals(id)){
			try {
				mgroup = sysservice.selectmGroup(id);
				for(MSyspara sys:mgroup.getListsyspara()){
					if("APP".equals(sys.getDatatype())){
						mssysapp = sys;
					}else if("DB".equals(sys.getDatatype())){
						mssysdb = sys;
					}
				}
			} catch (NumberFormatException e) {
			}
			
			//app user
			host = mssysapp.getOsip();
			user = mssysapp.getOsuser();
			pwd = mssysapp.getOspwd();
			dir = mssysapp.getLogdir();
			try {
				port = Integer.valueOf(mssysdb.getOsport());
			} catch (NumberFormatException e) {
			}
		}
		String ret = "";
		try {
			String status = request.getParameter("status1");
			String commands = request.getParameter("commands");
			command.append("ll;");
			if("linuxcommand".equals(status) && commands!=null){
				command = new StringBuffer();
				command.append(commands);
			}else if("ziplog".equals(status)){
				commandstr = "cd "+dir+";zip -r tbsserverlog"+datetime+".zip "+dir;
				ret =ret+ jc.sshExecute(host, user, pwd,commandstr, port);
				
				commandstr ="scp "+dir+"/tbsserverlog"+datetime+".zip root@"+mssysapp.getTbsmonitorosip()+":"+mssysapp.getLogzipdir();
				ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
				
				commandstr =" rm "+dir+"/*.zip; ";
				ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);

			}else if("remove".equals(status)){
				commandstr =" rm "+mssysapp.getLogzipdir()+"/*.*; ";
				ret =ret+ jc.sshExecute(host, user, pwd, commandstr, port);
			}
		} catch (JSchException e) {
			ret = e.getMessage();
		}
		
		
		request.setAttribute("dir", dir);
		request.setAttribute("command", ret);
		request.setAttribute("id", id);
		request.setAttribute("listmgroup",  sysservice.selectAllMGroup());
		request.setAttribute("mssysapp", mssysapp);
		request.setAttribute("mssysdb", mssysdb);
		
		request.getRequestDispatcher("logview.jsp").forward(request,response); 
		return;
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		sysservice = (SystemparameterService)Connections.getServiceBean(request, "sysparaService");
		doGet(request, response);
	}
	
	public List<SystemParameterBean> getListSysSetting(HttpServletRequest request){
		List<SystemParameterBean> listsysbean = new ArrayList<SystemParameterBean>();
		try {
			Connection conn = Connections.getConnection(request);
			String SQL = "SELECT * FROM SYSSETTING";
			PreparedStatement stmt = conn.prepareStatement(SQL);
			ResultSet rs = stmt.executeQuery();
			while (rs.next()) {
				SystemParameterBean sysb = new SystemParameterBean();
				sysb.setId(rs.getInt("Id"));
				sysb.setName(rs.getString("Name"));
				sysb.setType(rs.getString("Type"));
				sysb.setValue(rs.getString("Value"));
				sysb.setEnabled(rs.getString("Enabled"));
				sysb.setDescription(rs.getString("Description"));
				listsysbean.add(sysb);
			}
			
			rs.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return listsysbean;
	}

}
